import datetime, logging, os

from google.appengine.ext import db
from google.appengine.ext.webapp import template
from google.appengine.ext.db import NotSavedError, Error

from google.appengine.ext.db import djangoforms

# Create your models here.

class Event(db.Model):
    """
    Basic event from which everything else will hang from
    """
    name = db.StringProperty(required=True)
    
    # where the people are meeting
    place = db.PostalAddressProperty()

    author = db.StringProperty()
    link = db.LinkProperty()
    
    phone = db.PhoneNumberProperty()
    
    description = db.TextProperty()

    # exact date of the event
    event_date = db.DateTimeProperty()
    
    privacy = db.StringProperty()
    
    # Other properties
    participants = db.IntegerProperty(default=0)
    maybes = db.IntegerProperty(default=0)
    
    # Audit fields
    dt_created = db.DateTimeProperty(auto_now_add=True)
    dt_last_updated = db.DateTimeProperty()
    
    # comments
    comments = db.ListProperty(db.Key)
    


class EventForm(djangoforms.ModelForm):
    class Meta:
        model = Event
        # exclude items go here!
        exclude = ('dt_last_updated', 
                   'participants',
                   'ext_id',
                   'link',
                   'maybes',
                   #'event_date', 
                   'comments',
                   'description',
                   'privacy',
                   )    

class Participant(db.Model):
    """
    Participant of the event
    """    
    nickname = db.StringProperty()
    first_name = db.StringProperty()
    last_name = db.StringProperty()
    email = db.EmailProperty()
    
    # external id for system integration
    ext_id = db.StringProperty()
    
    # determines if it is : active vs inactive
    state = db.StringProperty(required=True)
    pic_src = db.URLProperty()
    
    notes = db.TextProperty()
    
    #birthday of the user
    bday = db.StringProperty()
    
    # audit fields
    dt_created = db.DateTimeProperty(auto_now_add=True)
    dt_last_updated = db.DateTimeProperty()
    


class Evite(db.Model):
    """
    Actual evite
    """
    
    # one-to-many relationships
    user = db.ReferenceProperty(Participant,
                                 collection_name='users_evites') 
    
    state = db.StringProperty(required=True, 
                              choices=set(["yes", "no", "maybe"]))
    
    #one-to-many relationships
    event = db.ReferenceProperty(Participant,
                                 collection_name='evites') 
     
    
    # audit fields
    dt_created = db.DateTimeProperty(auto_now_add=True)
    dt_last_updated = db.DateTimeProperty()
    
    # user id
    
    
    
class Comment(db.Model):
    """
    List of comments
    """    
    title = db.StringProperty(required=True)
    text = db.TextProperty(required=True)
    dt_created = db.DateTimeProperty(auto_now_add=True)
